package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StuMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;

    @Override
    public PageBean queryList(Integer clazzId, Integer degree, String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<Student> list = stuMapper.getAllStudents(clazzId,degree,name);
        return new PageBean(list.getTotal(),list.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        stuMapper.deleteByIds(ids);
    }

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        stuMapper.add(student);
    }

    @Override
    public Student getById(Integer id) {
        Student student = stuMapper.getStudentById(id);
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        stuMapper.update(student);
    }

    @Override
    public void updateScore(Integer id, Integer score) {
        stuMapper.redScore(id,score);
    }
}
